using System;
using System.Text.RegularExpressions;

namespace IValidate
{
	public class ValidateEmailAttribute : ModelValidatorBaseImpl
	{
		public ValidateEmailAttribute(string errorMessage)
			: this(errorMessage, true)
		{
		}

		public ValidateEmailAttribute(string errorMessage, bool required)
			: base(errorMessage, required)
		{
		}

		public override bool Validate(object value)
		{
			if (value == null) return false;
			if (this.Required && String.IsNullOrEmpty(value.ToString())) return false;
			Regex regex = new Regex(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
			return regex.IsMatch(value.ToString());
		}
	}
}
